package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentCheckGoodQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment, String[] tags) {
        if (strategyComment.getId() != null) {
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        } else {
            // 保存当前评论用户
            strategyComment.setUser(UserContext.getCurrentUser());
            // 保存评论
            strategyCommentMapper.insert(strategyComment);

            // 保存标签
            for (String name : tags) {
                Tag tag = new Tag();
                tag.setName(name);
                tagMapper.insert(tag);
                // 中间表关联
                strategyCommentMapper.insertRelation(tag.getId(), strategyComment.getId());
            }
        }
    }

    @Override
    public StrategyComment selectCommentById(Long id) {
        return strategyCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void checkThumbup(Long commentId, Long userId) {
        StrategyComment result=strategyCommentMapper.selectCheckThumbup(commentId,userId);
        if (result == null) {
            throw new RuntimeException("未点赞");
        }
    }

    @Override
    public void insertThumbup(Long commentId, Long currentUserId) {
        strategyCommentMapper.insertThumbupRelation(commentId,currentUserId);
        strategyCommentMapper.updateAddThumbupCount(commentId);
    }

    @Override
    public void delete1Thumbup(Long commentId, Long currentUserId) {
        strategyCommentMapper.delete1ThumbupRelation(commentId,currentUserId);
        strategyCommentMapper.updateReduceThumbupCount(commentId);
    }

    @Override
    public PageInfo selectCheckGoodInformation(StrategyCommentCheckGoodQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectStrategyCommentCheckHistory(qo);
        return new PageInfo(list);
    }

}
